-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear Queue implementation using array with rearrange elements.cpp
90 lines (79 loc) · 2.24 KB
/
Linear Queue implementation using array with rearrange elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// It's an alternative programing of Circular Queue that implemented in Linear Queue.
// More described at the line #80
#include <iostream>
using namespace std;
const int n = 5;
int A[n] = {0}, front = -1, rear = front;
int full(){
return (rear >= n-1);
}
int empty(){
return (front == -1);
}
void rearrange(){
if(front > 0){
int e = (rear-front)+1; // available elements
int x = front;
for (int i = 0; i < e; i++){
A[i] = A[x++];
if(i == 0) front = i;
if(i == e-1) rear = i;
}
}
}
void push(int data){
if(full()){
cout << "[QUEUE IS FULL]! Element [" << data << "] not pushed into queue." << endl;
} else {
rear = (front == -1) ? ++front : ++rear;
A[rear] = data;
}
}
void pop(){
if(empty()){
cout << "[QUEUE IS EMPTY]! No element popped." << endl;
} else if(front == rear){
front = rear = -1;
} else {
front++;
rearrange();
}
}
void print(){
if(front == -1){
cout << "[EMPTY QUEUE]" << endl;
} else {
while(!empty()){
cout << A[front];
pop();
if(!empty()) cout << " <= ";
}
cout << endl;
}
}
int main(){
push(5);
push(10);
push(15);
push(20);
push(25);
pop();
pop();
/*
* After dequeuing an element we can't access that position by enqueuing process until the queue is completely empty.
* From the above example (or previous program) our queue size is 5,
* we enqueued 5 elements and dequeued 2 element then we should have 2 free spaces to add element.
* Like if we add an element below after deque an element in the previous program
* enqueue(30);
* ↳ It will say [QUEUE IS FULL]! ... ...
* Because after dequeuing element(s), the existing elements position weren't rearranged,
* and the last element staying at last index so it says queue is full.
* To avoid this problem we called rearrange method after every dequeuing an element at the line #43,
* so existing element will be shifted to the front (or lower index).
*
* Now we can enqueue as many elements as have been dequeued.
*/
push(30);
push(35);
print();
}